//
//  HandOverProceVC.m
//  koubei
//
//  Created by 李彦宏 on 2018/5/4.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "HandOverProceVC.h"
#import "HandOverCell.h"//交接cell
#import "JWUserModel.h"
#import "JWUserManager.h"
#import "SDUserModel.h"//学生model
#import "HandOverModel.h"

@interface HandOverProceVC ()<UITableViewDelegate,UITableViewDataSource,HandOverDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *ContentArry;//交接流程内容
@property (nonatomic, strong) NSMutableArray *ContArryID;//打卡ID Arry
@property (nonatomic, strong) JWUserModel *JWmodel;
@property (nonatomic, strong) SDUserModel *SDmodel;
@property (nonatomic, strong) HandOverModel *Hmodel;//交接model
@property (nonatomic, strong) NSMutableArray  *SelecArry;//状态arry


@end

@implementation HandOverProceVC
//UItableView初始化
- (void)setupTable
{
    UITableView *tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    //隐藏组距导航栏的间距
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    tableView.backgroundColor=[UIColor whiteColor];
    tableView.showsHorizontalScrollIndicator=NO;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.estimatedRowHeight = 175;// 需要给cell高度估计值
    tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:tableView];
    self.tableView=tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.JWmodel=[JWUserManager me];
    self.SDmodel=[JWUserManager SDme];
     [self setdata];
    [self setupTable];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self setdata];
    }];
   
    // Do any additional setup after loading the view.
}
//GCD异步加载数据
-(void)setdata{
    __weak typeof(self) weakslf = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSDictionary *dic=@{
                            @"jwid":JWid,
                            @"jwpassword":JWpassword,
                            @"sid":self.SDmodel.sid
                            };

        [[AFNetworkTool shareManager]postJSONWithUrl:JW_HandOver parmas:dic successData:^(id json) {
            
            [self.tableView.mj_header endRefreshing];
            self.SelecArry=json[@"jiaojie"];
            
            NSLog(@"--json--%@", json);
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakslf.tableView reloadData];
            });
            
     
           
        } failure:^(NSError *error) {
            
            NSLog(@"--error--%@", error);
        }];
       
        [self.ContentArry removeAllObjects];
        NSArray *arry=self.JWmodel.jiaojielist;
        for(int i=0;i<arry.count;i++){
        self.Hmodel=[HandOverModel yy_modelWithJSON:arry[i]];
            [weakslf.ContentArry addObject:self.Hmodel];
        }
    });
}

#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ContentArry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    HandOverCell *cell=[HandOverCell cellwithTableview:tableView];
    
    cell.Handmodel=self.ContentArry[indexPath.row];
    cell.HandDeleget=self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    /**判断是否已经打卡
     */
    NSString *text=[self.SelecArry valueForKey:@"jiaojie"];
    
    NSLog(@"--text--%@", text);
    NSArray *p = [text componentsSeparatedByString:@","];
    if(text.length>0){

        for (int i = 0; i<p.count; i++) {
            if ([cell.Handmodel.contentID  isEqualToString:p[i]]) {

                [cell.SelectBtn setImage:[UIImage imageNamed:@"stu-ka"] forState:UIControlStateNormal];
                [self.ContArryID addObject:cell.Handmodel.contentID];
                [cell.SelectBtn setUserInteractionEnabled:NO];
            }
           [cell.SelectBtn setUserInteractionEnabled:YES];
        }
    }else{
        [cell.SelectBtn setUserInteractionEnabled:YES];
        
    }
    
    return cell;
}
-(void)clockAction:(UIButton *)send withModel:(HandOverModel *)model{
    if(model.Select){
        [self.ContArryID addObject:model.contentID];
    }else{
        [self.ContArryID removeObject:model.contentID];
    }
    [self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40;
}

#pragma -mark 尾部的内容
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footer=[[UIView alloc]init];
    footer.frame=CGRectMake(0, 0, ScreenWidth, 40);
    
    footer.backgroundColor=[UIColor whiteColor];
    UIButton *FootBtn=[[UIButton alloc]init];
    FootBtn.frame=CGRectMake(40, 5, ScreenWidth-80, 30);
    FootBtn.backgroundColor=dominantColor;
   
    [FootBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    FootBtn.layer.cornerRadius=5;
    FootBtn.showsTouchWhenHighlighted=YES;
    [FootBtn setTitle:@"确认流程" forState:UIControlStateNormal];
    [FootBtn addTarget:self action:@selector(FootBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    [footer addSubview:FootBtn];
    
    return footer;
}
//修改交接情况
-(void)FootBtnAction{
    NSLog(@"%@",self.ContArryID);
    NSSet *set = [NSSet setWithArray:self.ContArryID];
    
    
    NSString * content = [[set allObjects] componentsJoinedByString:@","];
    if ([content isEqualToString:@""]) {
        [MBProgressHUD showError:@"还未选取打卡内容"];
    }else{
        // 创建弹框控制器
        UIAlertController *alertCtry=[UIAlertController alertControllerWithTitle:@"交接流程" message:@"无法修改交接流程！！" preferredStyle:UIAlertControllerStyleAlert];
        
        //弹框的具体内容及响应事件
        UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction *addAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
          //确认响应事件
            NSDictionary *dic=@{
                                @"jwid":JWid,
                                @"jwpassword":JWpassword,
                                @"sid":self.SDmodel.sid,
                                @"jiaojie":content,
                                };
            [[AFNetworkTool shareManager]postJSONWithUrl:JW_ChangeHandOver parmas:dic successData:^(id json) {
                [self setdata];
                NSLog(@"--json--%@", json);
                [MBProgressHUD showSuccess:@"确认成功"];
                
            } failure:^(NSError *error) {
                NSLog(@"--error--%@", error);
            }];
            
        }];
        //添加弹框事件到弹框ctry中
        [alertCtry addAction:cancelAction];
        [alertCtry addAction:addAction];
        //把alertCtry 弹框视图推出来
        [self presentViewController:alertCtry animated:YES completion:nil];

    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSMutableArray *)ContentArry{
    if(_ContentArry==nil){
        _ContentArry=[[NSMutableArray alloc]init];
        
    }
    return _ContentArry;
}
-(NSMutableArray *)ContArryID{
    if(_ContArryID==nil){
        _ContArryID=[[NSMutableArray alloc]init];
    }
    return _ContArryID;
}
@end
